//
//  UatUtil.m
//  UStarter
//
//  Created by Mark on 2021/1/8.
//

#import "UatUtil.h"
#import <objc/runtime.h>

@implementation UatUtil

// 检测对象是否存在该属性
+ (BOOL)checkIsExistPropertyWithInstance:(id)instance verifyPropertyName:(NSString *)verifyPropertyName {
    unsigned int count, i;
    
    // 获取对象里的属性列表
    objc_property_t *properties = class_copyPropertyList([instance class], &count);
    
    for (i = 0; i < count; i++) {
        objc_property_t property =properties[i];
        //  属性名转成字符串
        NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        // 判断该属性是否存在
        if ([propertyName isEqualToString:verifyPropertyName]) {
            free(properties);
            return YES;
        }
    }
    free(properties);
    
    return NO;
}


+ (NSDictionary*) convertUrlStrToDictionary:(NSString*)urlStr ;{
    NSArray * arr = [urlStr componentsSeparatedByString:@"&"];
    NSInteger count=[arr count];
    if(arr==nil || count == 0){
        return @{};
    }
    NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:count];
    for (NSString*str in arr) {
        NSArray * kv = [str componentsSeparatedByString:@"="];
        NSInteger parseCount = [kv count];
        if(parseCount == 2){
            [dic setObject:kv[1] forKey:kv[0]];
        }else{
            DLog(@"属性:%@不合法",str);
            continue;
        }
    }
    return dic ;
}


+ (NSString*) componentUrl:(NSString*)originUrl param:(nullable NSDictionary*)param {
    NSString * c = [originUrl containsString:@"?"] ? @"&":@"?";
    
    NSArray * arr = [[param allKeys] enumarateForBlock:^id(id obj, NSInteger index, NSArray *arr) {
        
        // urlencode一下，否则空格、等特殊字符不会转义
        NSString*valStr =[[param objectForKey:obj] stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@"!*'\"();:@&=+$,/?%#[]% "] invertedSet]];
        return [NSString stringWithFormat:@"%@=%@",obj,valStr];
    }];
    NSString * paramStr = [arr componentsJoinedByString:@"&"];
    
    return [NSString stringWithFormat:@"%@%@%@",originUrl,c,paramStr];
}


+ (NSString*) combineHost:(nullable NSString*)host action:(NSString*)action ;{
    BOOL hasPrefix = [[action substringToIndex:3] isEqual:@"http"];
    if(hasPrefix){
        return action;
    }else{
        return [NSString stringWithFormat:@"%@%@",host,action];
    }
}


+ (BOOL)compareRemoteAppVersion:(NSString*)remoteVersion {
    
    NSString * localVersion = APP_VERSION;
    
    if(remoteVersion == nil){
        return true ;
    }
    NSArray<NSInteger> * remoteArr = [remoteVersion componentsSeparatedByString:@"."];
    NSArray<NSInteger> * localArr  = [localVersion componentsSeparatedByString:@"."];
    
    for (int i=0; i<remoteArr.count; i++) {
        if(localArr.count>i){
            NSInteger localInt = localArr[i];
            NSInteger remoteInt= remoteArr[i];
            if(remoteInt > localInt){
                return true ;
            }
        }
    }
    return false ;
}

@end
